package com.deepblue.recyclerviewtestdemo;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.deepblue.recyclerviewtestdemo.paker.IBaseView;
import com.deepblue.recyclerviewtestdemo.paker.IloadMoreView;
import com.deepblue.recyclerviewtestdemo.paker.RecyclerViewAdapterPaker;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private View headView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerViewAdapterPaker recyclerViewAdapterPaker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidget();
    }

    private void initWidget() {
        /**
         * SwipeRefreshLayout里面需要注意的Api：
         1、setOnRefreshListener(OnRefreshListener listener)  设置下拉监听，当用户下拉的时候会去执行回调
         2、setColorSchemeColors(int... colors) 设置 进度条的颜色变化，最多可以设置4种颜色
         3、setProgressViewOffset(boolean scale, int start, int end) 调整进度条距离屏幕顶部的距离
         4、setRefreshing(boolean refreshing) 设置SwipeRefreshLayout当前是否处于刷新状态，一般是在请求数据的时候设置为true，在数据被加载到View中后，设置为false。
         */
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
        mSwipeRefreshLayout.setColorSchemeColors(R.color.colorPrimary, R.color.colorAccent, R.color.colorPrimaryDark);
        /**
         * 下拉监听——刷新完了就可以让那个进度条消失
         */
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });

//        mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(R.color.colorAccent);
        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//listview样式
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));//gridview样式 两行

//        View view = LayoutInflater.from(this).inflate(R.layout.tv, null);
//        mRecyclerView.addView(view);
        //
        ArrayList<String> arrayList = getData();
        //
        MyAdapter myAdapter = new MyAdapter(this, arrayList);
        //添加头headview
        recyclerViewAdapterPaker = new RecyclerViewAdapterPaker(myAdapter, MainActivity.this);
        recyclerViewAdapterPaker.setCanLoadMore(true);
        recyclerViewAdapterPaker.setIsEnd(true);
        //加载更多
        recyclerViewAdapterPaker.setMoreView(new IloadMoreView() {
            @Override
            public void onLoading() {
                Toast.makeText(MainActivity.this, "onLoading", Toast.LENGTH_LONG).show();
                Log.w("tag", "onLoading");
            }

            @Override
            public void onFinish() {
                Toast.makeText(MainActivity.this, "onFinish", Toast.LENGTH_LONG).show();
                Log.w("tag", "onFinish");
            }

            @Override
            public void onBindView(boolean isEnd) {
                Toast.makeText(MainActivity.this, "onBindView_isEnd", Toast.LENGTH_LONG).show();
                Log.w("tag", "onBindView_isEnd");
            }

            @Override
            public void onBindView() {
                Toast.makeText(MainActivity.this, "onBindView", Toast.LENGTH_LONG).show();
                Log.w("tag", "onBindView");
            }

            @Override
            public View onCreateView(ViewGroup parent) {
                Toast.makeText(MainActivity.this, "onCreateView", Toast.LENGTH_LONG).show();
                Log.w("tag", "onCreateView");
                TextView tv = new TextView(MainActivity.this);
                tv.setText("加载更多");
                return tv;
            }
        });
//        recyclerViewAdapterPaker.setLoadMore(new RecyclerViewAdapterPaker.ILoadMore() {
//            @Override
//            public void loadMore() {
////                initData();
//                Toast.makeText(MainActivity.this, "loadMore", Toast.LENGTH_LONG).show();
//                recyclerViewAdapterPaker.setLoadMoreComplete();
////                recyclerViewAdapterPaker.setIsEnd(true);//no_more
//            }
//        });
        recyclerViewAdapterPaker.setHead(new IBaseView() {
            @Override
            public void onBindView() {

            }

            @Override
            public View onCreateView(ViewGroup parent) {
                headView = LayoutInflater.from(parent.getContext()).inflate(R.layout.head_test, parent, false);
                headView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "头", Toast.LENGTH_LONG).show();
                    }
                });
                return headView;
            }
        });
        //
        mRecyclerView.setAdapter(myAdapter);
        mRecyclerView.setAdapter(recyclerViewAdapterPaker);
    }

    private ArrayList<String> getData() {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("环境监控的沙发了客家话");
        arrayList.add("没的说了奶粉了看见审单发货看电视剧");
        arrayList.add("了肯德基风格算了可费劲了");
        arrayList.add("是丹佛文件分类看身份你们了深刻的");
        arrayList.add("和离婚了的身份房水库将加快了");
        arrayList.add("环境监控的沙发了客家话");
        arrayList.add("没的说了奶粉了看见审单发货看电视剧");
        arrayList.add("了肯德基风格算了可费劲了");
        arrayList.add("是丹佛文件分类看身份你们了深刻的");
        arrayList.add("和离婚了的身份房水库将加快了");
        arrayList.add("环境监控的沙发了客家话");
        arrayList.add("没的说了奶粉了看见审单发货看电视剧");
        arrayList.add("了肯德基风格算了可费劲了");
        arrayList.add("是丹佛文件分类看身份你们了深刻的");
        arrayList.add("和离婚了的身份房水库将加快了");
        arrayList.add("环境监控的沙发了客家话");
        arrayList.add("没的说了奶粉了看见审单发货看电视剧");
        arrayList.add("了肯德基风格算了可费劲了");
        arrayList.add("是丹佛文件分类看身份你们了深刻的");
        arrayList.add("和离婚了的身份房水库将加快了");
        arrayList.add("环境监控的沙发了客家话");
        arrayList.add("没的说了奶粉了看见审单发货看电视剧");
        arrayList.add("了肯德基风格算了可费劲了");
        arrayList.add("是丹佛文件分类看身份你们了深刻的");
        arrayList.add("和离婚了的身份房水库将加快了");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        arrayList.add("旅客的少年恢复了可");
        return arrayList;
    }
}
